// http.js
import axios from 'axios';
 import {config } from "@/common/config"
const http = axios.create({
  baseURL: config.url,
  timeout: 1000*30,
  headers: {
    'Source-Type': 'WEBSITE'
  },
});
// 请求拦截器
http.interceptors.request.use(config => {

  return config;
}, error => {
  return Promise.reject(error);
});
 
// 响应拦截器
http.interceptors.response.use(response => {
  const res = response.data;
  if (res.code !== 200) {
   
    if (res.code === 501 && !tokenAbnormal) {
      tokenAbnormal = true;
      MessageBox.confirm("页面失效是否重新登录！", {
        confirmButtonText: "重新登录",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          clearLoginInfo();
          setTimeout(() => {
            tokenAbnormal = false;
          }, 100);
          router.push({ name: "login" });
        })
        .catch(() => {
          tokenAbnormal = false;
        });
    }
    return Promise.reject(new Error(res.msg || "Error"));
  } else {
    return response.data;
  }

}, error => {
  return Promise.reject(error);
});
 
export default {
  get(url, params) {
    return http.get(url, {
      params: params
    });
  },
  post(url, data) {
    return http.post(url, data);
  },
  // 可以根据需要添加更多的方法，例如put, delete
};